package com.miyake.dsp.filter;

import com.miyake.dsp.ComplexArray;
import com.miyake.dsp.ScilabExecute;

public class FirScilab implements Filter {
	private ComplexArray source;
	private FilterSetting setting;
	
	public FirScilab(FilterSetting setting) {
		this.setting = setting;
	}

	private String getCommand() {
		ScilabCommand command = new ScilabCommand();
		String cmd = "[h,hm,fr]=wfir(" + 
		command.getFilterType(setting.getFilterType()) + ",33," + 
		command.getCutoff(setting.getCutoff1(), setting.getCutoff2(), setting.getFrequency()) + "," + 
		command.getWindowType(setting.getWindowType()) + "," + 
		"[0 0]" + 
		");hz=syslin('d',poly(h,'z','c')./%z**33);";
		
		return cmd;
	}

	@Override
	public void setSource(ComplexArray source) {
		this.source = source;
	}

	@Override
	public ComplexArray getSource() {
		return this.source;
	}

	@Override
	public ComplexArray getProcessed() {
		String command = getCommand() + "yhz=flts(data1,hz);";
		return ScilabExecute.execute(command, source, "yhz");
	}

	@Override
	public double[] getMagnitude(int point) {
		String command2 = "[hzm,fr]=frmag(hz," + String.valueOf(point) + ");";
		return ScilabExecute.execute(getCommand() + command2, source, "hzm").getRealPart();

	}

	@Override
	public double[] getPhase(int point) {
		String command2 =  "[hzm,fr]=frmag(hz,"+ String.valueOf(point) + ");pha=atan(hzm);";
		return ScilabExecute.execute(getCommand() + command2, source, "pha").getRealPart();

	}

	@Override
	public Object getSetting() {
		return setting;
	}
}
